package com.example.springpethospital.controller;

import com.example.springpethospital.entity.AdminMethod;
import com.example.springpethospital.entity.Illcase;
import com.example.springpethospital.service.*;
import com.example.springpethospital.util.Utility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/illManage")
@CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*")
public class IllcaseController {
    @Autowired
    IllcaseService illcaseService;
    @Autowired
    UserService userService;

    @GetMapping("/illSearch")
    public Object illSearch(HttpServletRequest request) {
        String name = request.getParameter("name");
        String sPaging = request.getParameter("paging");
        if (sPaging == null) {
            return Utility.newResponseObject(true, illcaseService.GetCases(name, null));
        } else {
            int paging = Integer.parseInt(sPaging);
            return Utility.newResponseObject(true, illcaseService.getTotalPages(name), illcaseService.GetCases(name, paging));
        }
    }

    @AdminMethod
    @PostMapping("/illAdd")
    public Object illAdd(HttpServletRequest request) {
        Illcase illcase = Illcase.getCaseFromRequest(request);
        if (illcaseService.AddCase(illcase))
            return Utility.newResponseObject(true);
        else
            return Utility.newResponseObject("插入失败");
    }

    @AdminMethod
    @PostMapping("/illModify")
    public Object illModify(HttpServletRequest request) {
        Illcase illcase = Illcase.getCaseFromRequest(request);
        if (illcaseService.ModifyCase(illcase))
            return Utility.newResponseObject(true);
        else
            return Utility.newResponseObject("修改失败，可能是当前病例已删除");
    }

    @AdminMethod
    @PostMapping("/illRemove")
    public Object illRemove(HttpServletRequest request) {
        try {
            String sid = request.getParameter("id");
            int id = Integer.parseInt(sid);
            if (illcaseService.RemoveCase(id))
                return Utility.newResponseObject(true);
            else
                return Utility.newResponseObject("删除失败，可能是当前病例已删除");
        } catch (NumberFormatException ex) {
            return Utility.newResponseObject("id格式不正确");
        }
    }
}
